const app = getApp();
Page({
  data: {
    currentTab: 9, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    isHidden: true,
    msg: '正在加载...', 
    pageIndex: 1,
    pageTotal: 0,
    tasksList: [],
    taskTypeList: []
  },
  onPullDownRefresh: function () {
    console.log('onPullDownRefresh');
    // this.data.pageIndex = 1;
    // this.data.tasksList = [];
    this.setData({
      pageIndex: 1,
      tasksList: []
    });
    this.getTasksList();
    wx.stopPullDownRefresh()
  },
  onReachBottom: function () {
    console.log('onReachBottom');
    var _this = this;

    _this.setData({
      isHidden: false
    });

    if (_this.data.pageIndex >= _this.data.pageTotal) {
      console.log('completed');
      setTimeout(function () {
        _this.setData({
          isHidden: true,
          msg: '达到底部'
        });
      }, 1000);
    } else {
      setTimeout(function () {
        console.log('loading');
        _this.data.pageIndex++;
        _this.getTasksList();
      }, 1000);
    }

  },
 
  giveUpDoTask: function (e) {

    var _this = this;
    let projectName = e.currentTarget.dataset.projectName;

    wx.showModal({
      content: '说好不抛弃不抛弃，为什么你这么无情这么残酷这么无理取闹~',
      success: function (res) {
        if (res.confirm) {

          let taskId = e.currentTarget.dataset.taskId;
          let projectTypeId = e.currentTarget.dataset.projectTypeId;
          wx.request({
            url: app.globalData.apiDomain + "crowdsource/task/giveUpTask/" + taskId,
            header: { 'Content-Type': 'application/json', 'token': app.globalData.token },
            method: 'POST',
            success: function (res) {
              if (res.data.status === 'ok') {
                _this.setData({
                  pageIndex: 1,
                  tasksList: []
                });
                // _this.data.pageIndex = 1;
                // _this.data.tasksList = [];
                _this.getTasksList();
              } else {
                app.responseCheck(_this, res.data, '/pages/my_tasks/task_list/task_list');
              }
            },
            fail: function (e) {
              wx.showModal({
                content: '不好，挂了！管理员哥哥正在赶来~',
                showCancel: false,
              })
            }
          })

        } else if (res.cancel) {
          
        }
      }
    })
  },
  continueDoTask: function (e) {
    
    let taskId = e.currentTarget.dataset.taskId;
    let projectTypeId = e.currentTarget.dataset.projectTypeId;
    let projectName = e.currentTarget.dataset.projectName;

    if (projectTypeId == 2) {
      console.log(taskId);
      wx.navigateTo({
        url: '../../public_tasks/do_task/do_task?id=' + taskId + '&projectName=' + projectName,
      })
    } else if (projectTypeId == 3) {
      console.log(taskId);
      wx.navigateTo({
        url: '../../public_tasks/do_picture_task/do_picture_task?id=' + taskId + '&projectName=' + projectName,
      })
    }
  },
  tasktap: function (e) {

    wx.navigateTo({
      url: '../../public_tasks/task_detail/task_detail?id=' + e.currentTarget.dataset.identify,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    //_this.data.taskTypeList = [];
    //_this.data.taskTypeList.push({ key: null, value: "全部任务" });

    wx.request({
      //url: app.globalData.apiDomain + "crowdsource/lookup/getLookupList/TASK_STATUS",
      url: app.globalData.apiDomain + "crowdsource/lookup/getMyTaskStatusList",
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        if (res.data.status === 'ok') {
          var list = res.data.object;
          for (var i = 0; i < list.length; i++) {
            _this.data.taskTypeList.push({
              key: list[i].id,
              value: list[i].displayname
            });
          }
          _this.setData({
            taskTypeList: _this.data.taskTypeList
          });
        } else {
          app.responseCheck(_this, res.data, '/pages/my_tasks/task_list/task_list');
        }
      },
      fail: function (e) {
        console.log('tasklist fail');
        wx.showModal({
          title: '获取任务类型失败',
          showCancel: false,
          content: e,
          success: function (res) {
            if (res.confirm) {
            }
          }
        })
      }
    })
  },

  // 加载任务列表
  getTasksList: function () {

    var _this = this;
    var data = {};

    //var status = _this.data.taskTypeList[_this.data.activeTypeIndex].key;
    var status = _this.data.currentTab;
    if (status !== null) {
      data.status = status;
    }

    data.pageNo = _this.data.pageIndex;
    wx.request({
      url: app.globalData.apiDomain + "crowdsource/task/getMyTasks",
      method: 'POST',
      data: data,
      header: { 'Content-Type': 'application/json', 'token': app.globalData.token },
      success: function (res) {
        if (res.data.status === 'ok') {
          //console.log(res);
          var task = res.data.object;
          //console.log(task);
          if (task.pages >= _this.data.pageIndex) {
            _this.data.tasksList = _this.data.tasksList.concat(task.list);
            _this.data.pageTotal = task.pages;
            _this.setData({
              isHidden: true,
              msg: '正在加载...',
              tasksList: _this.data.tasksList
            });
          }
        } else {
          app.responseCheck(_this, res.data, '/pages/my_tasks/task_list/task_list');
        }
      },
      fail: function (e) {
        wx.showModal({
          content: '网络异常,请重试',
          showCancel: false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    if (app.globalData.token == null) {
      app.login(_this, '/pages/my_tasks/task_list/task_list')
    } else {
      // this.data.pageIndex = 1;
      // this.data.tasksList = [];
      this.setData({
        pageIndex: 1,
        tasksList: []
      });
      this.getTasksList();
    }

    app.userLoginReadyCallback = res => {
      // this.data.pageIndex = 1;
      // this.data.tasksList = [];
      this.setData({
        pageIndex: 1,
        tasksList: []
      });
      this.getTasksList();
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTab == cur){ 
      return;
    }else {
      this.setData({
        tasksList: [],
        pageIndex: 1,
        currentTab: cur
      });
      if (this.data.currentTab == 12 || this.data.currentTab == 16) {
        this.setData({
          scrollLeft: 300
        })
      } else {
        this.setData({
          scrollLeft: 0
        })
      }
    }
    this.getTasksList();
  },
})